package com.boilerCloud.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.Dictionary;
import com.boilerCloud.dto.sys.DictionaryTypeInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.sys.IDictionaryService;


@RestController
@RequestMapping("/dictionary/")
public class DictionaryControler {
	private static final Logger log = LoggerFactory.getLogger(DictionaryControler.class);
	@Resource
	private IDictionaryService service;


	@RequestMapping(value = "dictionaryList")
	public ModelAndView dictionaryList() {
		return new ModelAndView("/views/sys/dictionary.html");
	}
	
	

	/**
	 * 查询字典列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryDictionaryList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryDictionaryList(@ModelAttribute Dictionary model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<Dictionary> page = new HzlqswPage<Dictionary>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询字典列表失败");
			log.error("查询字典列表异常",e);
			return rst;
		}
	}
	/**
	 * 查询字典列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryDictionaryById", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryDictionaryById(int id, HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			Dictionary dictionary = service.qryDictionaryById(id);
			rst.setReturnObject(dictionary);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
			
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询字典列表失败");
			log.error("查询字典列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改字典信息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editDictionary", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editDictionary(
			@ModelAttribute Dictionary model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改字典信息失败");
			log.error("修改字典信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增字典
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addDictionary", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addDictionary(
			@ModelAttribute Dictionary model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			Dictionary code = service.qryDictionaryByCode(model);
			if(code!=null){
				rst.setResultCode(ReqResult.resultCode_exist);
				rst.setReturnObject("字典代码不能重复,请重新填写");
				return rst;
			}
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增字典信息失败");
			log.error("新增字典信息异常",e);
			e.printStackTrace();
			return rst;
		}
	}
	
	/**
	 * 删除字典
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delDictionary", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除字典失败");
			log.error("删除字典异常",e);
			return rst;
		}
	}
	

	
	/**
	 * 根据类型获取字典列表
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getDictionaryList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getDictionaryList(
			@RequestParam(required = true) int type,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		
		try {
			List<Dictionary> list = service.getDictionaryList(type);
			rst.setReturnObject(list);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("根据类型获取字典列表失败");
			log.error("根据类型获取字典列表异常",e);
			return rst;
		}
	}

	/**
	 * 根据类型获取字典列表
	 * 
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping( value = "getDictionary2Type", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8" )
	public ReqResult getDictionary2Type( @ModelAttribute DictionaryTypeInfo dicModel, HttpServletRequest request ) {
		ReqResult rst = new ReqResult();

		try {
			List<DictionaryTypeInfo> list = service.getDictionary2Type(dicModel);
			rst.setReturnObject(list);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;

		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("根据类型获取字典列表失败");
			log.error("根据类型获取字典列表异常", e);
			return rst;
		}
	}
	

}
